<?php


namespace app\api\controller;


use app\api\service\JwtAuth;
use app\api\service\OrderServer;
use app\common\model\Users;
use think\facade\Db;
use think\facade\Request;

class Order extends Base
{
    /**
     * 控制器中间件 [登录、注册 不需要鉴权]
     * @var array
     */
    protected $middleware = [
        'app\api\middleware\Api' => ['except' => ['index', 'lists', 'otherOrderList']],
    ];


    public function index()
    {

    }

    public function lists()
    {
        $parms = $this->request->param();
        $where = [];
        $where[] = ['status', '=', 1];
        if ($parms['status'] >= 0 && $parms['status'] !== '') {
            $where[] = ['order_status', '=', $parms['status']];
        }

        $user = Users::find($this->getUid());
        if ($user->type_id == 2) {
            $where[] = ['master_id', '=', $user->id];
        }else{
            $where[] = ['user_id', '=', $user->id];
        }

        $list = Db::name('Order')
            ->where($where)
            ->order('order_status')
            ->order('order_time', 'desc')
            ->paginate((int)$parms['page_size'], (int)$parms['page_no']);
        return $this->result($list, 0, 1, '');
    }

    public function create()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->getUid();
        $result = OrderServer::create($param);
        if ($result) {
            return $this->result([], 0, 1, '提交成功');
        } else {
            return $this->result([], 0, 0, '提交失败');
        }
    }

    public function update()
    {
        $parms = $this->request->param();
        $result = OrderServer::update($parms, $this->getUid());
        if ($result) {
            return $this->result([], 0, 1, '提交成功');
        } else {
            return $this->result([], 0, 0, '提交失败');
        }
    }

    public function completeOrder()
    {
        $parms = $this->request->param();
        $result = OrderServer::complete($parms, $this->getUid());
        if ($result) {
            return $this->result([], 0, 1, '完成成功');
        } else {
            return $this->result([], 0, 0, '完成失败');
        }
    }

    public function delete()
    {
        $parms = $this->request->param();
        $result = OrderServer::delete($parms, $this->getUid());
        if ($result) {
            return $this->result([], 0, 1, '删除成功');
        }
    }

    public function detail()
    {
        $parms = $this->request->param();
        $result = OrderServer::detail($parms['id'], $this->getUid());
        return $this->result($result, 0, 1, '');
    }

    /**
     * 获取用户id
     * @return mixed
     */
    protected function getUid()
    {
        $jwtAuth = JwtAuth::getInstance();
        return $jwtAuth->getUserId();
    }

    public function otherOrderList()
    {
        $parms = $this->request->param();
        $where = [];
        $where[] = ['status', 'in', 1];
        $where[] = ['order_status', 'in', [0, 1]];

        $list = Db::name('Order')
            ->where($where)
            ->order('order_status')
            ->order('order_time', 'desc')
            ->paginate((int)$parms['page_size'], (int)$parms['page_no']);
        return $this->result($list, 0, 1, '');
    }

    public function accept()
    {
        $parms = $this->request->param();
        $result = OrderServer::acceptOrder($parms, $this->getUid());
        return $this->result($result, 0, 1, '');
    }

    public function masterAcceptedOrders()
    {
        $parms = $this->request->param();
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['master_id', '=', $this->getUid()]; // 当前师傅ID
        if ($parms['status'] >= 0 && $parms['status'] !== '') {
            $where[] = ['order_status', '=', $parms['status']];
        }

        $list = Db::name('Order')
            ->where($where)
            ->order('pick_up_time', 'desc') // 按接单时间排序
            ->paginate((int)$parms['page_size'], (int)$parms['page_no']);

        return $this->result($list, 0, 1, '');
    }

}
